package site.shader85.Kingdoms.domain;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.bukkit.Location;
import org.bukkit.configuration.serialization.ConfigurationSerializable;
import org.bukkit.configuration.serialization.SerializableAs;
import org.bukkit.entity.Entity;

import com.google.common.collect.Lists;
import com.google.common.collect.Maps;

import site.shader85.Kingdoms.domain.area.Area;

@SerializableAs("Manor")
public class Manor implements ConfigurationSerializable{

	private Domain owner;

	private String name;
	private List<String> users = Lists.newArrayList();
	private Area area;

	public String getName() {
		return this.name;
	}
	
	public Domain getOwner() {
		return owner;
	}

	public Area getArea() {
		return area;
	}
	
	private Manor(String n, Domain d, Area a) {
		name = n;
		owner = d;
		area = a;
	}

	@Override
	public boolean equals(Object obj) {
		if (obj instanceof Manor) {
			return name.equals(((Manor) obj).name);
		}
		return false;
	}

	@Override
	public int hashCode() {
		return name.hashCode();
	}

	public static Manor createManor(String n, Domain d, Area a) {
		return new Manor(n, d, a);
	}

	@Override
	public Map<String, Object> serialize() {
		HashMap<String, Object> table = Maps.newHashMap();
		table.put("area", area);
		table.put("name", name);
		table.put("owner", owner.getName());
		table.put("users", users);
		return table;
	}
	
	@SuppressWarnings("unchecked")
	public static Manor deserialize(Map<String, Object> table) {
		String name = (String)table.get("name");
		String owner = (String)table.get("owner");
		Area area = (Area)table.get("area");
		List<String> users = (List<String>)table.get("users");
		Domain d = Domain.getDomain(owner);
		Manor m = Manor.createManor(name, d, area);
		for(String n:users){
			m.users.add(n);
		}
		return m;
	}
	
	public boolean inManor(Entity e) {
		return inManor(e.getLocation());
	}

	public boolean inManor(Location loc) {
		return area.inArea(loc);
	}

	public boolean inManor(Area a) {
		return area.inArea(a);
	}
	
}
